# 自媒体发表文章对象库层
import allure
from selenium.webdriver.common.by import By

from Base.MediaBase.baseMedia import BasePage, BaseHandle
from utils import UtilsDriver, choose_channel


class PublishPage(BasePage):
    def __init__(self):
        super().__init__()
        self.driver = UtilsDriver.get_media_driver()
        # 标题
        self.title = By.CSS_SELECTOR, "[placeholder='文章名称']"
        # 内容
        self.iframe_part = By.CSS_SELECTOR, "#publishTinymce_ifr"
        self.content = By.CSS_SELECTOR, "#tinymce"
        # 封面
        self.cover = By.XPATH, "//*[@role='radiogroup']/label[4]/span[2]"
        # 频道
        self.channel = By.CSS_SELECTOR, "[placeholder='请选择']"
        # 发表
        self.publish_button = By.CSS_SELECTOR, "[class='el-button filter-item el-button--primary']"

    #    找到标题
    def find_title(self):
        return self.get_element(self.title)

    #    找到内容
    def find_frame(self):
        return self.get_element(self.iframe_part)

    def find_content(self):
        return self.get_element(self.content)

    # 找到封面
    def find_cover(self):
        return self.get_element(self.cover)

    # 找到频道
    def find_channel(self):
        return self.get_element(self.channel)

    # 找到发表
    def find_publish(self):
        return self.get_element(self.publish_button)


# 自媒体发表文章操作层
class PublishHandle(BaseHandle):
    def __init__(self):
        self.publish_page = PublishPage()

    #     填写标题
    @allure.step(title="填写标题")
    def send_title(self, title):
        self.input_text(self.publish_page.find_title(), title)

    #         填写内容
    @allure.step(title="填写内容")
    def send_content(self, content):
        self.publish_page.driver.switch_to.frame(self.publish_page.find_frame())
        self.input_text(self.publish_page.find_content(), content)
        # self.publish_page.driver.switch_to_default_content()
        self.publish_page.driver.switch_to.default_content()

    #          选择封面
    @allure.step(title="选择封面")
    def click_cover(self):
        self.publish_page.find_cover().click()

    #       选择频道
    @allure.step(title="选择频道")
    def click_channel(self, channel):
        choose_channel(self.publish_page.driver, self.publish_page.find_channel(), channel)

    #       点击发表
    @allure.step(title="点击发表")
    def click_publish(self):
        self.publish_page.find_publish().click()


# 自媒体发表文章业务层
class PublishProxy():
    def __init__(self):
        self.publish_handle = PublishHandle()

    def publish_article(self, title, content, channel):
        self.publish_handle.send_title(title)
        self.publish_handle.send_content(content)
        self.publish_handle.click_cover()
        self.publish_handle.click_channel(channel)
        self.publish_handle.click_publish()
